# Maintainer: omni <omni+alpine@hack.org>
pkgname=uutils-coreutils
_pkgname=coreutils
pkgver=0.0.26
pkgrel=0
pkgdesc="coreutils rewritten in Rust"
# s390x: blocked by nix crate
arch="all !s390x"
url="https://github.com/uutils/coreutils"
license="MIT"
makedepends="cargo help2man oniguruma-dev cargo-auditable"
checkdepends="coreutils" # test_ls.rs incompatible with busybox truncate
subpackages="$pkgname-doc"
source="$pkgname-$pkgver.tar.gz::https://github.com/uutils/coreutils/archive/$pkgver.tar.gz"
builddir="$srcdir/$_pkgname-$pkgver"
options="!check" # package builder file descriptor issues

export RUSTONIG_DYNAMIC_LIBONIG=1

prepare() {
	default_prepare

	cargo fetch --target="$CTARGET" --locked
}

build() {
	cargo auditable build --frozen \
		--profile=release-small --features=feat_os_unix_musl

	mkdir bin
	cp target/release-small/"$_pkgname" bin/uutils

	mkdir man1
	help2man --no-info --name=uutils --manual=uutils \
		--version-string="$pkgver" bin/uutils > man1/uutils.1

	# create symlinks and individual man pages
	for uutil in $(./bin/uutils | tail -n +7 | tr -d '\n' | sed -e 's/,//g' -e 's/sha3-\s*512sum/sha3-512sum/'); do
		ln -s uutils bin/uutils-$uutil
		case "$uutil" in
			test) ;; # helpless
			\[) help2man --no-info --name=uutils-test --manual=uutils \
				bin/uutils-"$uutil" > man1/uutils-test.1 ;;
			uniq) help2man --no-info --name=uutils-"$uutil" --manual=uutils \
				--no-discard-stderr \
				bin/uutils-"$uutil" > man1/uutils-"$uutil".1 ;;
			*) help2man --no-info --name=uutils-"$uutil" --manual=uutils \
				bin/uutils-"$uutil" > man1/uutils-"$uutil".1 ;;
		esac
	done
}

check() {
	ulimit -n 4096 # running out of file descriptors

	cargo test --frozen \
		--profile=release-small --features=feat_os_unix_musl
}

package() {
	mkdir -p "$pkgdir"/usr
	mv bin "$pkgdir"/usr

	mkdir -p "$pkgdir"/usr/share/man
	mv man1 "$pkgdir"/usr/share/man
	install -Dm0644 LICENSE -t "$pkgdir"/usr/share/licenses/"$pkgname"
}

sha512sums="
ba3eb06b4a11ba1adbe28fd53282827a148a3dd616db06230f7d31810418fd80f024e55ecd29ff2556b307f146bd99b083bd337912a900e7190db1c32bdf5cb0  uutils-coreutils-0.0.26.tar.gz
"
